package _05_stack.my_implements.exerc;

import org.junit.Test;

import java.util.LinkedList;

/**
 * @author: mornd
 * @dateTime: 2023/6/7 - 16:14
 *
 * 使用一个队列来模拟栈
 */
public class MyStack2 {
    @Test
    public void test() {
        Stack stack = new Stack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }

    static class Stack {
        /*
            1,2,3

            2 1 3   132 321
            1,2,3,4

            2,1,3   1,3,2, 3,2,1
         */
        LinkedList<Integer> queue = new LinkedList();

        public void push(int x) {
            int c = queue.size();
            queue.offer(x);
            while (c-- > 0) { // 循环次数 = 插入前队列的size，size可自己维护或者使用队列的
                queue.offer(queue.poll());
            }
        }

        public int pop() {
            return (int) queue.pop();
        }

        public int top() {
            return (int) queue.peek();
        }

        public boolean empty() {
            return queue.isEmpty();
        }
    }
}
